import {Component, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {Location} from '@angular/common';
import {NzMessageService} from 'ng-zorro-antd';
import {FormBuilder, FormGroup} from '@angular/forms';
import {SignupService} from '../signup.service';
import {SignupSetting} from '../signup-setting';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'app-addsetting',
    templateUrl: './addsetting.component.html',
})
export class AddsettingComponent implements OnInit {

    signupsetting: SignupSetting = {};
    signFrom: FormGroup;

    constructor(
        private singupService: SignupService,
        private fb: FormBuilder,
        private msgService: NzMessageService,
        private location: Location,
        private activitRouter: ActivatedRoute,
    ) {
    }

    ngOnInit() {
        this.signFrom = this.fb.group({
            title: [''],
            signupPrice: [''],
            trainePrice: [''],
            totalPrice: [''],
            type: [''],
        });

        const id = this.activitRouter.snapshot.paramMap.get('id');
        if (id) {
            this.singupService.getSettingInfo(+id).subscribe(res => {
                this.signupsetting = res;

                this.signFrom.patchValue({
                    title: this.signupsetting['titile'],
                    signupPrice: this.signupsetting['signupPrice'],
                    trainePrice: this.signupsetting['trainePrice'],
                    totalPrice: this.signupsetting['totalPrice'],
                    type: this.signupsetting['type'],
                });
            }, error1 => {
            });
        }
    }


    save() {
        const val = this.signFrom.value;
        this.signupsetting['titile'] = val['title'];
        this.signupsetting['signupPrice'] = val['signupPrice'];
        this.signupsetting['trainePrice'] = val['trainePrice'];
        this.signupsetting['totalPrice'] = val['totalPrice'];
        this.signupsetting['type'] = val['type'];

        this.singupService.addSetting(this.signupsetting).subscribe(res => {
                this.location.back();
                this.msgService.success('保存成功');
            }, error1 => {

            }
        );
    }
}
